var newForm = {};

newForm.popularMark = true;
newForm.popularModel = true;
newForm.mark = '';
newForm.model = '';
newForm.fadeTime = 150;
newForm.city = '';
newForm.drivers = '';
newForm.carAge = '';
newForm.carPower = '';
newForm.carPrice = '';
newForm.requirePhone = '';
newForm.hasInsurance = '';
newForm.endInsurance = null;
newForm.endInsuranceView  = '';

newForm.loadCars = function (popular) {
	forbiddenCalculation = true;
	$('#nFTitle').attr('src', './nFSelectMark.gif').css('top', '65px');
	$('.popularModels').remove();
	$('.popularModelsSelected').remove();
	$('.allModels').remove();
	$('.allModelsSelected').remove();
	newForm.popularMark = popular;
	$('#newFormContent').fadeOut(newForm.fadeTime, newForm.lc);
	$('#h-phones-in').html(newForm.counter2);
}

newForm.lc = function () {
	var totalMarks = 0;
	$('#chooseAllModels').remove();
	newForm.requirePhone = $('#requirePhone').text();
	if ($('#newForm').find('.popularMarksSelected').length == 0 && $('#newForm').find('.popularMarks').length == 0) $('#newFormContent').before('<div class="popularMarksSelected" onclick="newForm.loadCars(true)"></div><div  class="allMarks" onclick="newForm.loadCars(false);" style=""></div>').css('width', '88%');
	if (newForm.popularMark) {
		$('.popularMarks').attr('class', 'popularMarksSelected');
		$('.allMarksSelected').attr('class', 'allMarks');
	} else {
		$('#newForm').find('.popularMarksSelected').attr('class', 'popularMarks');
		$('#newForm').find('.allMarks').attr('class', 'allMarksSelected');
	}
	try {
		var marks;
		newForm.model = '';
		if (newForm.popularMark) {
			$('#newFormContent').html('').css('padding-left', '80px');
			marks = carsXML.find('m').filter(function () {
				return $(this).attr('favorite') == 'favorite' || $(this).attr('favorite') == 'popular'
			});
		} else {
			$('#newFormContent').html('').css('padding-left', '80px');
			marks = carsXML.find('m');
		}
		var chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'V', 'W', 'X', 'Z', 'В', 'Г', 'З', 'И', 'У'];
		if (newForm.popularMark) {
			$('#newForm').css('height', '520px');
			var marksHtml = '<div style="float:left;width:540px;margin-top:20px;">'
			for (var i = 23; i < 28; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nFRM"><tr><td style="width:15px;text-align:center"><b></b></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td>';
					}
					table += '</tr></table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div><div style="float:left;width:180px;">'
			for (var i = 0; i < 7; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nFEM"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div><div style="float:left;width:180px;">'
			for (var i = 7; i < 14; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nFEM"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div><div style="float:left;width:180px;">'
			for (var i = 14; i < 23; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nFEM"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div>';
		} else {
			$('#newForm').css('height', '890px');
			var marksHtml = '<div style="float:left;width:580px;margin-top:20px;"><table class="nFRM" style="margin-top:5px;margin-bottom:18px;width:auto; display:inline-block;"><tr align="left">';
			for (var i = 23; i < 28; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<td style="width:93px;text-align:left"><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<td style="width:93px;text-align:left"><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td>';
					}
					marksHtml += table;
				}
			}
			marksHtml += '</tr></table></div><div style="float:left;clear:left;width:140px;">';
			for (var i = 0; i < 5; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nfEMT"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div><div style="float:left;width:140px;">'
			for (var i = 5; i < 11; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nfEMT"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div><div style="float:left;width:140px;">'
			for (var i = 11; i < 16; i++) {
				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nfEMT"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}
			marksHtml += '</div><div style="float:left;width:140px;">'
			for (var i = 16; i < 23; i++) {

				var foundMarks = marks.filter(function () {
					return $(this).find('n:first').text().charAt(0) == chars[i];
				});
				totalMarks += foundMarks.length;
				if (foundMarks.length > 0) {
					table = '<table class="nfEMT"><tr><td style="width:15px;text-align:center"><img alt="" src="./letters/' + chars[i] + '.gif"/></td><td><a href="#">' + foundMarks.eq(0).find('n:first').text() + '</a></td></tr>';
					for (var k = 1; k < foundMarks.length; k++) {
						table += '<tr><td></td><td><a href="#">' + foundMarks.eq(k).find('n:first').text() + '</a></td></tr>';
					}
					table += '</table>';
					marksHtml += table;
				}
			}

			marksHtml += '</div>';
		}
		$('#newFormContent').append(marksHtml).find('td>a').click(function () {
			$('#newFormContent').find('td>a').attr('class', '');
			$(this).attr('class', 'nFSelectedMark');
			newForm.mark = $(this).text();
			newForm.toModels(true);
			return false;
		});
		if ($('#chooseAllMarks').length == 0) $('#newForm').after('<a id="chooseAllMarks" href="#" onclick="newForm.loadCars(false);return false;" style="text-decoration:none;border-bottom:1px solid;">Если Вы не нашли марку автомобиля, кликните "Все марки", откроется полный список (144)</a>');
		newForm.selectedMark();
		$('#newFormContent').fadeIn(newForm.fadeTime);
		$('#newFormContent').css('display', 'block').css('margin', '0');

	} catch (ee) {
		alert(ee)
	};
}

newForm.selectedMark = function () {
	if (newForm.mark != '') $('#newFormContent').find('td>a').attr('class', '').filter(function () {
		return $(this).text() == newForm.mark;
	}).attr('class', 'nFSelectedMark');
}

newForm.toModels = function (popular) {
	newForm.popularModel = popular;
	$('#newFormContent').fadeOut(newForm.fadeTime, newForm.tm);
}

newForm.tm = function () {
	$('#chooseAllMarks').remove();
	var markIsPopular = carsXML.find('m').filter(function () {
		return $(this).find('n:first').text() == newForm.mark;
	});
	var models = markIsPopular.find('c'); //toyota 87
	var totalModels = models.length;
	if (markIsPopular.attr('favorite') == 'popular' || markIsPopular.attr('favorite') == 'favorite') markIsPopular = true;
	else
	markIsPopular = false;
	var markRightMenuText = '';
	var requireFavorites = false;
	if (markIsPopular && models.length > 36) {
		requireFavorites = true;
		$('#nFTitle').attr('src', './nFSelectModel.gif').css('top', '65px');
	} else
	$('#nFTitle').attr('src', './nFSelectModel.gif').css('top', '85px');

	$('.popularMarks').remove();
	$('.popularMarksSelected').remove();
	$('.allMarks').remove();
	$('.allMarksSelected').remove();
	if (($('.popularModelsSelected').length == 0 || $('.popularModels').length == 0) && requireFavorites) $('#newFormContent').before('<div class="popularModelsSelected" onclick="newForm.toModels(true)"></div><div  class="allModels" onclick="newForm.toModels(false);" style=""></div>');
	if (requireFavorites && newForm.popularModel) {
		$('.popularModels').attr('class', 'popularModelsSelected');
		$('.allModelsSelected').attr('class', 'allModels');
	} else
	if (requireFavorites) {
		$('.popularModelsSelected').attr('class', 'popularModels');
		$('.allModels').attr('class', 'allModelsSelected');
	}
	try {
		if (requireFavorites && newForm.popularModel) {
			models = models.filter(function () {
				return $(this).find('n:first').attr('p') == 'p'
			});
		}

		var cars = '<div class="nFMdlD" style="margin-left:15px;">';
		var i = 0
		var carsPerRow = models.length % 3 == 0 ? parseInt(models.length / 3) : parseInt(models.length / 3) + 1;
		if (carsPerRow > 12) {
			$('#newForm').css('height', (450 + (carsPerRow - 12) * 25) + 'px');
			//		alert('carsPerRow='+carsPerRow+'\n'+(560+(carsPerRow-20)*31)+'px'+'\nh='+$('#newForm').css('height'));
		} else
		$('#newForm').css('height', '460px');
		models.each(function () {
			cars += '<li class="nFMdl"><span>' + $(this).text().replace('*', 'Другая модель') + '</span></li>';
			i++;
			if (i % carsPerRow == 0 && models.length != i) cars += '</div><div class="nFMdlD">';
		});
		cars = cars + '</div>';
		newForm.updateRightMenu();
		var rightCol = '<table cellpadding=0 cellspacing=0 style="width:100%;height:100%" valign="top"><tr><td valign="top" style="">' 
		+ cars + '</td><td style="width:250px;height:100%" valign="top" rowspan="2"><div id="rightMenu" style=""></div></td></tr><tr><td style="">'
		+ '<a onclick="newForm.loadCars(newForm.popularMark);return false;" style="cursor:pointer;margin:50px 0 0 22px;;float:left;"><img alt="" src="back.gif"></a><a id="nFNextStep" onclick="if(newForm.validateCar())newForm.toCarParameters();return false;" style=""><img alt="" src="next.gif"></a></td></tr></table>';
		$('#newFormContent').css('width', '100%').css('padding-left', '0px').html(rightCol);
		newForm.clearCars();
		$('#newFormContent').find('li').click(function () {
			newForm.model = $(this).text();
			newForm.clearCars();
			$(this).attr('class', 'nFMdlS');
			newForm.updateRightMenu();
			newForm.toCarParameters();
		});
		$('#newFormContent').find('li').filter(function () {
			return $(this).text() == newForm.model;
		}).attr('class', 'nFMdlS');
		newForm.updateRightMenu();
		//	$('#newFormTitles').remove();

	} catch (eee) {
		alert(eee)
	};
	if (requireFavorites) {
		if ($('#chooseAllModels').length == 0) $('#newForm').after('<a href="#" id="chooseAllModels" style=" text-decoration:none;border-bottom:1px solid" onclick="newForm.toModels(false);return false;">Если Вы не нашли модель автомобиля, кликните "Все модели", откроется полный список</a>');
	} else
	$('#chooseAllModels').remove();
	$('#newFormContent').fadeIn(newForm.fadeTime);
}

newForm.validateCar = function () {
	if (newForm.model == '') {
		alert('\nУкажите, пожалуйста, модель вашего автомобиля\n\n');
		return false;
	}
	return true;
}

newForm.clearCars = function () {
	$('#newFormContent').find('li').attr('class', 'nFMdl');
}

newForm.removePopularButtons = function () {
	nf = $('#newForm');
	nf.find('.popularMarks').remove();
	nf.find('.popularMarksSelected').remove();
	nf.find('.allMarks').remove();
	nf.find('.allMarksSelected').remove();
	nf.find('.popularModelsSelected').remove();
	nf.find('.popularModels').remove();
	nf.find('.allModelsSelected').remove();
	nf.find('.allModels').remove();
}

newForm.toDrivers = function () {

	newForm.removePopularButtons();
	$('#newFormContent').fadeOut(newForm.fadeTime, newForm.td);
	$('#nFTitle').attr('src', './nfSelectDrivers.gif').css('top', '85px');

}

newForm.td = function () {
	$('#chooseAllModels').remove();
	$('#chooseAllMarks').remove();
	$('#newFormContent').html('');
	if (newForm.city == '') newForm.checkCity();
	$('#newForm').css('height', '580px');

	var nextStep = 'calculate';
	var nextIMG = 'nfCalculate.gif';
	if (newForm.requirePhone == '1') {
		nextStep = 'toPhoneForm';
		nextIMG = 'next.gif';
	}
	var leftTd = '';
	leftTd += '<img id="nFSelectCity" alt="" src="./nFSelectCity.gif"/><br><input type="radio" onclick="newForm.city=\'msc\';newForm.updateRightMenu();"' + (newForm.city == 'msc' ? 'checked' : '') + ' id="newFormCityMSC" name="newFormCity"><label class="nFTowmSelect" for="newFormCityMSC">&nbsp;Москва</label><input ' + (newForm.city == 'spb' ? 'checked' : '') + ' type="radio" onclick="newForm.city=\'spb\';newForm.updateRightMenu();" id="newFormCitySPB" name="newFormCity"><label class="nFTowmSelect" for="newFormCitySPB">&nbsp;Санкт-Петербург</label><div class="nFHRDrivers"></div>';
	leftTd += '<div id="newFormDriverList" style=""></div><div id="driversButtons"><img src="./img/ico-cancel.gif" id="nFCancelImg"style=""><span id="nNMltdrv" onclick="newForm.toMultiDrive()">Не указывать водителей,<br>выбрать мультидрайв</span><img onclick="newForm.addDriver()" id="nFDDrvr"src="./img/nFButton-add-driver.gif"/></div>';
	var rightCol = '<table cellpadding=0 cellspacing=0 style="width:100%;height:100%" valign="top"><tr><td valign="top" style=""><div style="width: 436px;height:100%;">' + leftTd + '</div></td><td style="width:250px;height:100%" valign="top" rowspan="2"><div id="rightMenu" style=""></div></td></tr><tr><td style="height:70px;background:"><a onclick="newForm.toCarParameters();return false;" style="cursor:pointer;margin:50px 0 0 22px;;float:left;"><img alt="" src="back.gif"></a><a id="nFNextStep" onclick="if(newForm.validateDrivers())newForm.' + nextStep + '();return false;" style=""><img alt="" src="' + nextIMG + '"></a></td></tr></table>';

	$('#newFormContent').html(rightCol);
	newForm.updateRightMenu();
	$('#newFormContent').fadeIn(newForm.fadeTime, function () {
		$('.nFDRG:first').focus();
	})
	//		alert(newForm.drivers);
	if (newForm.drivers == '') newForm.addDriver();
	else
	if (newForm.drivers != '') if (newForm.drivers == 'md') newForm.toMultiDrive();
	else
	for (var i = 0; i < newForm.drivers.length; i++) {
		newForm.addDriver();
		var inputs = $('.nFDrvrR').eq(i).find('input');
		inputs.eq(0).val(newForm.drivers[i][0]);
		inputs.eq(1).val(newForm.drivers[i][1]);
	};
}

newForm.addDriver = function () {
	var drList = $('#newFormDriverList');
	if (newForm.drivers == 'md') {
		$('#driversButtons').html('<img src="./img/ico-cancel.gif" id="nFCancelImg"style=""><span id="nNMltdrv" onclick="newForm.toMultiDrive()">Не указывать водителей,<br>выбрать мультидрайв</span><img onclick="newForm.addDriver()" id="nFDDrvr"src="./img/nFButton-add-driver.gif"/>');
		drList.html('');
		newForm.drivers = '';
	}
	var c = 1;
	var drRow = "<div class='nFDrvrR'>" + "<p class='drCounter' style='clear:right;'>Водитель</p>" + "<div class='newFormAge'>" + "<div class='nFDRGL'>Возраст, лет:</div>" + "<div class='nFInputWrapper56'><input type='text' class='nFDRG dial_input' /></div>" + "</div>" + "<div style='margin:0px;float:right;position:relative;top:-24px;right:40px;' class='dial " + (drList.find('.nFDrvrR').length > 0 ? 'dialIE' : '') + "'></div>" + "<div style='' class='nFHRDrivers'></div>" + "<div style='height:120px;float:left;width:436px;'>" + "<div class='newFormExp'>" + "<div class='nFDREL'><nobr>Стаж вождения, лет:</nobr></div>" + "<div class='nFInputWrapper56E' style=''><input type='text' class='nFDRE dial_input' /></div>" + "</div>" + "<div class='dial' style='margin:0;position:relative;right:40px;;'></div>" + "</div>" + "<div style='' class='nFHRDrivers'></div>" + "</div>";
	drList.append(drRow);
	var count = drList.find('.nFDrvrR').length;
	if (count > 1) $('#newForm').css('height', (285 + 318 * count) + 'px');
	else
	$('#newForm').css('height', '600px');
	//	alert(count+'\n'+$('#newForm').css('height'));
	newForm.updateRightMenu();
	$('.dial').each(function () {
		newForm.makeDial(this)
	});
	$('.nFDrvrR').find('p').each(function () {
		$(this).text('Водитель ' + (c++) + ':')
	});
	$('.nFDRG').bind('change keypress keyup', function () {
		if ($(this).val().length > 2) $(this).val($(this).val()[2]);
		if (isNaN(parseInt($(this).val()))) $(this).val('');
		else $(this).val(parseInt($(this).val()));
	});
	$('.nFDRE').bind('change keypress keyup', function () {
		if ($(this).val().length > 2) $(this).val($(this).val()[2]);
		if (isNaN(parseInt($(this).val()))) $(this).val('');
		else $(this).val(parseInt($(this).val()));
	});
}

newForm.makeDial = function (id, len) {
	var div = $(id);
	if (typeof len == 'undefined') len = 2;
	div.html("<div class='dialNumber'>1</div><div class='dialNumber'>2</div><div class='dialNumber'>3</div>" + "<div class='dialNumber'>4</div><div class='dialNumber'>5</div><div class='dialNumber'>6</div>" + "<div class='dialNumber'>7</div><div class='dialNumber'>8</div><div class='dialNumber'>9</div>" + "<div class='dialZero'>0</div><div class='dialNumber'>&lt;</div>");
	div.children().mousedown(function () {
		if ($(this).attr('class') == 'dialNumber') $(this).attr('class', 'dialNumberPressed');
		else if ($(this).attr('class') == 'dialZero') $(this).attr('class', 'dialZeroPressed');
		newForm.dialNClick(this, len)
	}).bind('mouseup mouseout', function () {
		if ($(this).attr('class') == 'dialNumberPressed') $(this).attr('class', 'dialNumber');
		else if ($(this).attr('class') == 'dialZeroPressed') $(this).attr('class', 'dialZero');
	});

}

newForm.dialNClick = function (id, len) {
	if (typeof len == 'undefined') len = 2;

	var container = $(id).parent().parent();
	var targetInput = container.find('input.dial_input:first');
	var value = $(id).text();
	var targetVal = targetInput.val();
	if (len != 777) {
		if (isNaN(parseInt(targetVal))) {
			targetVal = '';
			targetInput.val('').trigger('change');
		}
		if (!isNaN(parseInt(value))) {
			if (targetVal.length < len || len > 2) {
				if (targetVal.length < len) targetInput.val(addSpaces(targetVal + value)).trigger('change');
				else
				targetInput.val(addSpaces(targetVal)).trigger('change');
			} else
			targetInput.val(addSpaces(value)).trigger('change');
		} else {
			//		alert(3);
			if (targetVal.length > 0) targetInput.val(addSpaces(targetVal.substring(0, targetVal.length - 1))).trigger('change');
		}
	} else {
		if (!isNaN(value)) {
			if ($('#phonePrefix').val().length < 3) $('#phonePrefix').val($('#phonePrefix').val() + value).keyup();
			else
			$('#phoneNumber').val($('#phoneNumber').val() + value).keyup();
		} else {
			if ($('#phoneNumber').val().length > 0) $('#phoneNumber').val($('#phoneNumber').val().substring(0, $('#phoneNumber').val().length - 1)).keyup();
			else
			if ($('#phonePrefix').val().length > 0) $('#phonePrefix').val($('#phonePrefix').val().substring(0, $('#phonePrefix').val().length - 1)).keyup();
		}

	}
}

newForm.toMultiDrive = function () {
	newForm.drivers = 'md';
	$('#newFormDriverList').html('<div id="selectedMd" style="">Вы выбрали опцию <span style="color: rgb(255, 114, 0); padding: 0px;"><b>«Мультидрайв»</b></span>.При наступлении страхового случая ущерб будет возмещен страховой компанией вне зависимости от того, кто находился за рулем.</div>');
	$('#driversButtons').html('<img onclick="newForm.addDriver()" id="nFDDrvr"src="./img/nFButton-add-driver.gif"/>');
	$('#nFDDrvr').css('margin', '5px 0 0 36px');
	$('#newForm').css('height', '560px');
	newForm.updateRightMenu();

}

newForm.toCarParameters = function () {
	$('#newFormContent').fadeOut(newForm.fadeTime, newForm.toCP);

}

newForm.toCP = function () {
	$('#chooseAllModels').remove();
	$('#chooseAllMarks').remove();
	newForm.removePopularButtons();
	$('#nFTitle').attr('src', './nFP.gif').css('top', '73px');
	$('#newForm').css('height', '630px');
	var leftTd = ''
	+ '<img alt="" src="./nFCarHasInsurance.gif" style="margin:25px 0 0 40px;">'
	+ '<div id="nFCE">'
		+ '<div style="cursor:default"></div><div data-val="yes">Да</div><div data-val="no">Нет</div>'
	+ '</div>'
	+ '<div id="DateEndInsuranceContainer" style="display: none;">'
		+ '<img alt="" src="./nFDateEndInsurance.gif" style="margin:0px 0px 0px 40px;">'
		+ '<div id="nFCB">'
			+ '<div class="DateWrapper">'
				+ '<input id="DateEndInsurancePicker" class="inputWrap" type="text">'
			+ '</div>'
		+ '</div>'
	+ '</div>'
	+ '<img alt="" src="./nFCarAgeHeader.gif" style="margin:0px 0px 0px 40px;">'
	+ '<div id="nFCG">'
	+ '<div style="cursor:default"></div><div>2011</div><div>2010</div><div>2009</div><div>2008</div><div>2007</div><div>2006</div><div>2005</div><div>2004</div><div>2003</div><div style="width:80px;margin-left:10px;">старше 2003</div>'
	+ '</div>'
	+ '<img src="./nFHorses.gif" alt="" style="margin-left:25px;"/>'
	+ '<div id="nFCP"><div style="cursor:default;width:16px;"></div><div>свыше 150</div><div>121-150</div><div>101-120</div><div>71-100</div><div>51-70</div><div>до 50</div></div>'
	+ '<img src="./nFPriceButton.gif" alt="" style="margin-left:25px;float:left;"/>'
	+ '<div class="dial" style="margin:0;padding:0;position:relative;right:130px;float:right"></div>'
	+ '<div id="nFPriceWrapper" class="PriceWrapper">'
	+ '<input type="text" id="nFCR" class="inputWrap dial_input"/>'
	+ '</div>'
	+ '<div class="nFCPInput" style="">&nbsp;руб</div>'
	+ '<div id="nFCPr">'
	+ '<p style="color:#717171;font-size:smaller;clear:left;padding-left:36px;padding-top:18px;width:150px;">Стоимость КАСКО напрямую зависит от стоимости машины.<br>Указывайте текущую стоимость автомобиля.<br>Уточнить её вы можете на <a target="blank" href="http://all.auto.ru">auto.ru</a></p>'
	+ '</div>'
	
// 	log(leftTd);
	
	var rightMenuText = newForm.rightMenu();

	var rightCol = '<table cellpadding=0 cellspacing=0 style="width:100%;height:100%" valign="top"><tr><td valign="top" style=""><div style="width:100%;height:100%;">' + leftTd + '</div></td><td style="width:250px;height:100%" valign="top" rowspan="2"><div id="rightMenu" style="">' 
	+ rightMenuText 
	+ '</div></td></tr><tr><td style=""><a onclick="newForm.toModels();return false;" style="cursor:pointer;margin:50px 0 0 22px;;float:left;"><img alt="" src="back.gif"></a><div id="nFNextStep"  onclick="if(newForm.validateCarParameters())newForm.toDrivers();return false;"><img alt="" src="next.gif"></div></td></tr></table>';
	
	$('#newFormContent').html(rightCol);

	$( "#DateEndInsurancePicker" ).datepicker({
			changeYear: true
		});
	
	$( "#DateEndInsurancePicker" ).change(function(){
			newForm.endInsuranceView = this.value;

 			if(!isDate(this.value)){
 				return;
 			}
			
			newForm.endInsurance     = this.value.split('.').reverse().join('-');
			newForm.updateRightMenu();				
		});

	$('#nFCE').find('div').mousedown(
		function () {
			if ($(this).text() != '') {
				if ($(this).attr('data-val') == 'yes') { 
					newForm.hasInsurance = 1;
					$('#DateEndInsuranceContainer').show();
					$(this).siblings().attr('class', '');
					$(this).attr('class', 'nFSctdG');
					$('#newForm').css('height', '700px');
				}
				else {
					newForm.hasInsurance = 0;
					$( "#DateEndInsurancePicker" ).val('');
					newForm.endInsurance = null;
					$('#DateEndInsuranceContainer').hide();
					$(this).siblings().attr('class', '');
					$(this).attr('class', 'nFSctdG');
					$('#newForm').css('height', '630px');
				}
				newForm.updateRightMenu();				
			}
	});
	
	$('#nFCG').find('div').mousedown(
		function () {
			if ($(this).text() != '') {
				if ($(this).text() != 'старше 2003') 
					newForm.carAge = parseInt($(this).text());
				else
					newForm.carAge = 2002;
				$(this).siblings().attr('class', '');
				$(this).attr('class', 'nFSctdG');
				newForm.updateRightMenu();
				newForm.updateCarPrice();
			}
	});
	
	$('#nFCP').find('div').mousedown(
		function () {
			if ($(this).text() != '') {
				switch ($(this).text()) {
				case 'свыше 150':
					newForm.carPower = 'power150more';
					break;
				case '121-150':
					newForm.carPower = 'power150';
					break;
				case '101-120':
					newForm.carPower = 'power120';
					break;
				case '71-100':
					newForm.carPower = 'power100';
					break;
				case '51-70':
					newForm.carPower = 'power70';
					break;
				case 'до 50':
					newForm.carPower = 'power50';
					break;
				}
				$(this).siblings().attr('class', '');
				$(this).attr('class', 'nFSctdP');
				newForm.updateRightMenu();
			}
	});
	$('.dial').each(function () {
		newForm.makeDial(this, 10)
	});

	if (newForm.endInsurance !== null) {				
		$('#DateEndInsurancePicker').val(newForm.endInsuranceView);
	}

	if (newForm.hasInsurance !== '') {
		var text;
		switch (newForm.hasInsurance) {
			case 0:
				text = 'no';
				break;
			case 1:
				text = 'yes';
				break;
		}
				
		$('#nFCE').find('div').filter(function () {
			return $(this).attr('data-val') == text;
		}).mousedown();
	}

	if (newForm.carAge != '') $('#nFCG').find('div').filter(function () {
		return $(this).text() == newForm.carAge || newForm.carAge == 2001 && $(this).text() == 'старше 2002'
	}).mousedown();

	if (newForm.carPower != '') {
		var t = '';
		switch (newForm.carPower) {
		case 'power50':
			t = 'до 50';
			break;
		case 'power70':
			t = '51-70';
			break;
		case 'power100':
			t = '71-100';
			break;
		case 'power120':
			t = '101-120';
			break;
		case 'power150':
			t = '121-150';
			break;
		case 'power150more':
			t = 'свыше 150';
			break;
		}
		$('#nFCP').find('div').filter(function () {
			return $(this).text() == t
		}).mousedown();
	}
	$('#nFCR').bind('keypress change keyup keydown blur mousedown mouseup', function () {
		$('#nFCR').val(addSpaces($('#nFCR').val()));
		newForm.carPrice = $('#nFCR').val();
		newForm.updateRightMenu()
	}).val(newForm.carPrice);
	newForm.updateRightMenu();
	$('#newFormContent').fadeIn(newForm.fadeTime);
}

newForm.validateCarParameters = function () {
	var text = ''; 
	if (newForm.hasInsurance === '') text += 'Укажите, пожалуйста наличие страховки\n\n';
	if (newForm.hasInsurance){
		if(newForm.endInsuranceView == '')	 
			text += 'Укажите, пожалуйста дату окончания страховки\n\n';
		else if(!isDate(newForm.endInsuranceView))
			text += 'Укажите, пожалуйста дату окончания страховки в правильном формате: ДД.ММ.ГГГГ\n\n';
	}
	if (newForm.carAge   == '') text += 'Укажите, пожалуйста возраст вашего автомобиля\n\n';
	if (newForm.carPower == '') text += 'Укажите, пожалуйста мощность двигателя вашего автомобиля\n\n';
	if (newForm.carPrice == '') text += 'Укажите, пожалуйста стоимость вашего автомобиля\n\n';
	if (text != '') {
		alert(text)
		return false;
	}
	return true;
}
newForm.validateDrivers = function () {
	var drList = new Array();
	var drivers = $('.nFDrvrR');
	var valid = true;
	var less18 = false;
	var invalidAgeExp = '';
	var emptyAgeExp = '';
	var md = newForm.drivers == 'md';
	var i = 0
	drivers.each(function () {
		i++;
		var tmp = new Array();
		var curVal = true;
		var inputs = $(this).find('input');
		tmp.push(parseInt(inputs.eq(0).val()));
		tmp.push(parseInt(inputs.eq(1).val()));
		if ((inputs.eq(0).val() == '' || inputs.eq(1).val() == '') && inputs.eq(0).val() != inputs.eq(1).val()) {

			if (inputs.eq(0).val() == '') emptyAgeExp += 'Водитель ' + i + ': не заполнено поле возраста\n';
			else
			emptyAgeExp += 'Водитель ' + i + ': не заполнено поле стажа вождения\n';
		}
		needCheck = (inputs.eq(1).val() != '' && inputs.eq(0).val() != '' || (inputs.eq(1).val() != '' ^ inputs.eq(0).val() != ''));
		if (tmp[0] < 18) less18 = true;
		if (((tmp[0] - tmp[1]) < 18)) {
			invalidAgeExp += 'Водитель ' + i + ': При возрасте ' + tmp[0] + ' лет невозможен стаж вождения ' + tmp[1] + ' лет.\n';
		}
		if ((tmp[0] < 18 || tmp[1] < 0 || (tmp[0] - tmp[1] < 18) || isNaN(tmp[0]) || isNaN(tmp[1])) && needCheck) {
			inputs.css('border-bottom', '1px solid red');
			valid = false;
		} else {
			if (inputs.eq(1).val() != '' && inputs.eq(0).val() != '' && tmp.length > 0) drList.push(tmp);
			inputs.css('border-bottom', '0px');
		}
	});
	if (valid && drList.length == 0 && !md) {
		alert('Укажите, пожалуйста, хотя бы 1 водителя\n');
		return false;
	}
	if (less18) {
		alert('Ошибка! Минимальный возраст для вождения - 18 лет');
		return false;
	}
	if (emptyAgeExp != '') {
		alert(emptyAgeExp);
		return false;
	}
	if (invalidAgeExp != '') {
		alert(invalidAgeExp);
		return false;
	}
	if (!valid || drList.length == 0 && !md && newForm.drivers.length == 0) {
		$('#newFormContent').fadeIn(newForm.fadeTime);
		newForm.updateRightMenu();
		return false;
	}
	if (drList.length > 0) newForm.drivers = drList;
	else if (md) newForm.drivers = 'md';
	return true;
}

newForm.toPhoneForm = function () {
	$('#newFormContent').fadeOut(newForm.fadeTime, newForm.toPH);
}

newForm.toPH = function () {
	$('#chooseAllModels').remove();
	$('#chooseAllMarks').remove();
	$('#nFTitle').attr('src', './nFMobileHeader.gif').css('top', '84px');
	$('#newFormContent').html('');
	$('#newForm').css('height', '475px');
	newForm.removePopularButtons();
	var leftTd = '<img src="./nFActivateCalculation.gif" style="margin:30px 0 20px 35px;" alt="">'
	+ '<p style="margin:0 35px;font-size:11px;font-family:verdana;">Укажите Ваш мобильный номер телефона, на который будет выслан результат расчета стоимости полиса КАСКО в 25 страховых компаниях.</p>'
	+ '<img alt="" src="./nFMobileIcon.gif" style="float:left;margin:30px 0 0 20px;"><div style="width:255px;float:left;margin:40px 0;"><div style="float:left;height:13px;padding:5px 0;">Моб. телефон: +7&nbsp;(&nbsp;</div><div id="nFMobilePrefix"><input class="nFPrefix" id="phonePrefix" type="text"></div><div style="float:left;height:13px;padding:5px;">&nbsp;)&nbsp;</div><div id="nFMobilePhone"><input id="phoneNumber" class="nPhoneNumber" type="text"></div></div><div class="dial1"></div>'
	+ '<p id="nFPFP1" style="">Данная система расчетов разработана и запатентована компанией KASAG.RU и не имеет аналогов в мире. Удобство заключается в доступности расчетов в любое время, стоит просто открыть SMS! Данный сервис абсолютно бесплатен для Вас.</p>'
	+ '<p id="nFPFP2" style=""><b>Услуга полностью бесплатна!</b></p><p id="phoneDublicate" style="">Результаты расчета будут отправлен на номер:<br><b>+7&nbsp;(<span style="color:#999999;">000</span>)</b>&nbsp;<b><span></span><span style="color:#999999">000-00-00</span></b></p>';
	var rightCol = '<table cellpadding=0 cellspacing=0 style="width:100%;height:100%" valign="top"><tr><td valign="top" style="height:490px"><div style="width:100%;height:100%;">' + leftTd + '</div></td><td style="width:250px;height:100%" valign="top" rowspan="2"><div id="rightMenu" style=""></div></td></tr><tr><td style=""><a onclick="newForm.toDrivers();return false;" style="cursor:pointer;margin:50px 0 0 22px;;float:left;"><img alt="" src="back.gif"></a><div id="nFNextStep" onclick="newForm.calculate();return false;"><img alt="" src="next.gif"></div></td></tr></table>';
	$('#newFormContent').html(rightCol);
	$('.dial1').each(function () {
		newForm.makeDial(this, 777)
	});
	newForm.updateRightMenu();
	$('#phonePrefix').bind('change', newForm.validatePhonePrefix);
	$('#phonePrefix').bind('keyup change', function (event) {
		newForm.onRegionChange(event);
	});
	$('#phoneNumber').bind('keyup', newForm.validatePhoneNumber);
	$('#newFormContent').fadeIn(newForm.fadeTime, function () {
		$('#phonePrefix').focus();
	});
}

newForm.updateRightMenu = function () {
	var rm = newForm.rightMenu();
	var h = parseInt($('#newForm').css('height')) - 13;
	var leftTdHeight = parseInt($('#newForm').css('height')) - 100;
	$('#newForm').find('td:first').css('height', leftTdHeight);
	$('#rightMenu').html(rm).css('height', h);
}

newForm.checkCity = function () {
	if ($('#mscTown').attr('checked')) newForm.city = 'msc';
	else
	newForm.city = 'spb';
}

newForm.validatePhonePrefix = function () {
	var validPrefs = [800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 903, 905, 906, 909, 910, 915, 916, 917, 919, 925, 926, 929, 936, 941, 962, 963, 964, 965, 967, 968, 970, 971, 985, 997];

	var pref = $('#phonePrefix').val();
	pref = pref.replace(/\D/g, '')
	pref = parseInt(pref);
	if (isNaN(pref) || pref < 100 || pref > 999) {
		$('#phonePrefix').css('color', 'red');
		return false;
	}
	var found = false;
	for (var i = 0; i < validPrefs.length; i++)
	if (validPrefs[i] == pref) {
		$('#phonePrefix').css('color', 'black');
		return true;
	}
	$('#phonePrefix').css('color', 'red');
	return false;
}

newForm.onRegionChange = function (event) {
	event = (event) ? event : window.event;
	var keycode = (event.charCode) ? event.charCode : event.keyCode;
	if (keycode == 9 || keycode == 16 || keycode == 17 || keycode == 18 || keycode == 46 || keycode > 36 && keycode < 41) return;
	var code = $('#phonePrefix').val();
	if (code.length >= 3) {
		code = code.substring(0, 3);
		$('#phonePrefix').val(code);
		$('#phoneDublicate').find('span:first').text(code).css('color', 'black');
		validatePhonePrefix();
		$('#phoneNumber').focus();
	}
}

newForm.validatePhoneNumber = function () {
	var code = $('#phoneNumber').val();
	code = code.replace(/\D/g, '')
	code = code.substring(0, 7);
	var newCode = '';
	$('#phoneNumber').val(code);
	switch (code.length) {
	case 0:
		newCode = '&nbsp;<b><span></span><span style="color:#999999">000-00-00</span></b>';
		break;
	case 1:
		newCode = '&nbsp;<b><span>' + code.substring(0, 1) + '</span>'
	+ '<span style="color:#999999">00-00-00</span></b>';
		break;
	case 2:
		newCode = '&nbsp;<b><span>' + code.substring(0, 2) + '</span>'
	+ '<span style="color:#999999">0-00-00</span></b>';
		break;
	case 3:
		newCode = '&nbsp;<b><span>' + code.substring(0, 3) + '</span>'
	+ '<span style="color:#999999">-00-00</span></b>';
		break;
	case 4:
		newCode = '&nbsp;<b><span>' + code.substring(0, 3) + '-' + code.substring(3, 4) + '</span>'
	+ '<span style="color:#999999">0-00</span></b>';
		break;
	case 5:
		newCode = '&nbsp;<b><span>' + code.substring(0, 3) + '-' + code.substring(3, 5) + '</span>'
	+ '<span style="color:#999999">-00</span></b>';
		break;
	case 6:
		newCode = '&nbsp;<b><span>' + code.substring(0, 3) + '-' + code.substring(3, 5) + '-' + code.substring(5, 6) + '</span>'
	+ '<span style="color:#999999">0</span></b>';
		break;
	case 7:
		newCode = '&nbsp;<b><span>' + code.substring(0, 3) + '-' + code.substring(3, 5) + '-' + code.substring(5, 7) + '</span>'
	+ '<span style="color:#999999"></span></b>';
		break;
	}
	var spans = $('#phoneDublicate').find('span');
	spans.eq(2).remove();
	spans.eq(1).remove();
	$('#phoneDublicate').find('b').eq(0).after(newCode);
	if (isNaN(code) || code.length != 7) return false;
	else
	return true;
}

newForm.updateCarPrice = function () {
	var year = new Date();
	year = year.getFullYear();
	year = parseInt(year) - parseInt(newForm.carAge);
	$.post("getPrice.php", {
		m: newForm.mark,
		ml: newForm.model,
		a: year
	}, function (data) {
		if (parseInt(data) > 10000) {
			$('#nFCR').val(addSpaces(data));
			newForm.carPrice = addSpaces(data);
		}
	});
}

newForm.rightMenu = function () {
	markRightMenuText = '<img alt="" style="float:left;margin:40px 120px 10px 0px" src="./nFCheckedBanner.gif"/>';
	var drText = '';
	if (newForm.mark != '') {
		markRightMenuText += '<div style="float:left;clear:left;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;width:110px;">Марка:</div><div onclick="newForm.loadCars(newForm.popularMark)" style="float:left;color:#717171;border-bottom:1px solid;cursor:pointer;">' + newForm.mark + '</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;"></div>';
		if (newForm.model != '') {
			markRightMenuText += '<div style="width:110px;float:left;clear:left;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Модель:</div><div onclick="newForm.toModels(newForm.popularModel)" style="color:#717171;border-bottom:1px solid;cursor:pointer;float:left;"><nobr>' + newForm.model + '</nobr></div><div style="width:100%;margin:0px;paddfing:0px;height:0px;"></div>';
			if (newForm.hasInsurance !== '') {
				markRightMenuText += '<div style=";float:left;clear:left;width:110px;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Cтраховка:</div> <div onclick="newForm.toCarParameters();" style="float:left;color:#717171;border-bottom:1px solid;cursor:pointer;">' + (newForm.hasInsurance == 1 ? 'Есть' : 'Нет') + '</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;"></div>';
			}
			if (newForm.endInsuranceView != '') {
				markRightMenuText += '<div style=";float:left;clear:left;width:110px;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Окончание:</div> <div onclick="newForm.toCarParameters();" style="float:left;color:#717171;border-bottom:1px solid;cursor:pointer;">' + newForm.endInsuranceView + '</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;"></div>';
			}
			if (newForm.carAge != '') {
				markRightMenuText += '<div style=";float:left;clear:left;width:110px;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Год выпуска:</div> <div onclick="newForm.toCarParameters();" style="float:left;color:#717171;border-bottom:1px solid;cursor:pointer;">' + (newForm.carAge > 2001 ? newForm.carAge : 'старше 2002') + '</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;"></div>';
			}
			if (newForm.carPower != '') {
				var t = '';
				switch (newForm.carPower) {
				case 'power50':
					t = 'до 50';
					break;
				case 'power70':
					t = 'от 51 до 70';
					break;
				case 'power100':
					t = 'от 71 до 100';
					break;
				case 'power120':
					t = 'от 101 до 120';
					break;
				case 'power150':
					t = 'от 121 до 150';
					break;
				case 'power150more':
					t = 'свыше 150';
					break;
				}
				markRightMenuText += '<div style="float:left;clear:left;width:110px;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Мощность<br>двигателя (л.с.):</div><div onclick="newForm.toCarParameters();" style="color:#717171;border-bottom:1px solid;float:left;border-bottom:1px soid;cursor:pointer;">' + t + '</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;border:0px;"></div>';
			}
			if (newForm.carPrice != '') markRightMenuText += '<div style="float:left;clear:left;width:110px;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Цена автомобиля:</div><div onclick="newForm.toCarParameters();" style="float:left;color:#717171;border-bottom:1px solid;cursor:pointer;">' + newForm.carPrice + '&nbsp;руб</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;border:0px;"></div>';
			if (newForm.city != '') markRightMenuText += '<div style="float:left;width:110px;clear:left;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Город:</div><div onclick="newForm.toDrivers(newForm.popularModel)" style="float:left;color:#717171;border-bottom:1px solid;cursor:pointer"><nobr>' + (newForm.city == 'msc' ? 'Москва' : 'Санкт-петербург') + '</nobr></div><div style="width:100%;margin:0px;paddfing:0px;height:0px;border:0px;"></div>';
			if (newForm.drivers != '') {
				if (newForm.drivers == 'md') {
					drText = '<span onclick="newForm.toDrivers();" style="color:#717171;border-bottom:1px solid;cursor:pointer;">Мультидрайв</span>';
				} else {
					for (var i = 0; i < newForm.drivers.length; i++)
					drText += '<nobr><span onclick="newForm.toDrivers();" style="color:#717171;border-bottom:1px solid;cursor:pointer;">' + newForm.drivers[i][0] + ' лет, стаж ' + newForm.drivers[i][1] + ' лет</span></nobr><br>';
				}
				markRightMenuText += '<div style="float:left;width:110px;clear:left;font-weight:bold;font-weight:bold;font-size:11px;margin-bottom:10px;">Водители:</div><div style="float:left;">' + drText + '</div><div style="width:100%;margin:0px;paddfing:0px;height:0px;"></div>';
			}
		}
	}
	return markRightMenuText;
}

newForm.loadToBaseForm = function () {
	//#1 марка
	$('#manufacturer-select').find('li').filter(function () {
		return $(this).text() == newForm.mark;
	}).click().mousedown();
	//#2 модель
	$('#car-select').find('li').filter(function () {
		return ($(this).text() == newForm.model) || newForm.model == 'Другая модель' && $(this).text() == '*';
	}).click().mousedown();
	//#3 возраст ТС
	$('#car-age-select').find('li').filter(function () {
		return $(this).text() == newForm.carAge;
	}).click().mousedown();
	//#4 двигатель
	$('#' + newForm.carPower).click().mousedown();
	//#5 водители
	if (newForm.drivers == 'md') setMultiDrive();
	else {
		for (var i = 1; i < newForm.drivers.length; i++)
		addDriverRowHTML();
		var drvrs = $('.f-driver');

		for (var i = 0; i < newForm.drivers.length; i++) {
			var nFDr = newForm.drivers[i];
			var dr = drvrs.eq(i);
			dr.find('input:first').val(nFDr[0]);
			dr.find('input').eq(1).val(nFDr[1]);
		}
	}
	//#6 цена
	$('#price').val(newForm.carPrice).change().keyup();
	//#7 город
	if (newForm.city == 'msc') $('#mscTown').attr('checked', 'checked');
	if (newForm.city == 'spb') $('#spbTown').attr('checked', 'checked');
}

newForm.calculate = function () {
	//alert ('!');
	if (newForm.requirePhone == '1') {
		var valid = true;
		if (!newForm.validatePhonePrefix()) valid = false;
		if (!newForm.validatePhoneNumber()) {
			valid = false;
			$('#phoneNumber').css('color', 'red');
		} else
		$('#phoneNumber').css('color', 'black');
		if (!valid) {
			alert('Мы обрабатываем только заявки из Москвы и Московской области!');
			return false;
		}
		phoneToSave = '+7 ' + $('#phonePrefix').val() + ' ' + $('#phoneNumber').val();
	} else
	phoneToSave = '';
	window.onbeforeunload = null;
	$('#sendMail').html('').css('display', 'none');
	$('#requirePhone').text(0);
	newForm.loadToBaseForm();
	forbiddenCalculation = false;
	$('#newForm').remove();
	$('#newFormTitles').remove();
	$('#VIPFirstNote').css('right', '0');
	var fields = $('#f-1').find('fieldset');
	fields.eq(0).css('display', '');
	fields.eq(1).css('display', '');
	fields.eq(2).css('display', '');

	sendPhone();

	calculateCasko(new Object);
	return false;
}
